package ru.niir.goodfil.excel;

import ru.niir.goodfil.audit.util.Assert;
import ru.niir.goodfil.excel.client.*;
import ru.niir.goodfil.excel.impl.DefaultColumnModel;
import ru.niir.goodfil.excel.impl.DefaultHeaderModel;
import ru.niir.goodfil.excel.impl.DefaultSheetModel;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: Кирилл
 */
public class SheetModelBuilder {
    private final String sheetTitle;

    private SheetModel sheetModel;

    private final List<ColumnModel> columnModels = new ArrayList<ColumnModel>();

    private final Set<String> ignoredColumns = new HashSet<String>();

    public SheetModelBuilder(String sheetTitle) {
        Assert.notBlank(sheetTitle);

        this.sheetTitle = sheetTitle;
    }

    public SheetModelBuilder addIgnoredColumn(String columnName) {
        Assert.notBlank(columnName);

        ignoredColumns.add(columnName);
        return this;
    }

    public SheetModelBuilder addColumn(String columnName, String columnHeader) {
        Assert.notBlank(columnName);
        Assert.notNull(columnHeader);

        ColumnModel columnModel = new DefaultColumnModel(columnName);
        columnModel.setHeaderModel(new DefaultHeaderModel(columnHeader));
        columnModels.add(columnModel);
        return this;
    }

    public SheetModelBuilder setColumnHeaderModel(HeaderModel headerModel) {
        getLastColumnModel().setHeaderModel(headerModel);
        return this;
    }

    public SheetModelBuilder setColumnRenderer(CellRenderer renderer) {
        getLastColumnModel().setRenderer(renderer);
        return this;
    }

    public SheetModelBuilder setColumnWidth(int width) {
        getLastColumnModel().setWidth(width * 256);
        return this;
    }

    public SheetModelBuilder setColumnValueExtractor(ValueExtractor extractor) {
        getLastColumnModel().setExtractor(extractor);
        return this;
    }

    private ColumnModel getLastColumnModel() {
        Assert.notNull(columnModels);
        Assert.isTrue(columnModels.size() >= 1);

        return columnModels.get(columnModels.size() - 1);
    }

    public SheetModel getSheetModel() {
        SheetModel sheetModel = new DefaultSheetModel(sheetTitle, columnModels);
        return sheetModel;
    }
}
